/**
 *
 * @param array
 * @param fromIndex
 * @param toIndex
 * @return {array}
 *
 * examples
 *  const array = [1,2,3,4,5,6,7]
 *  move(array, 2, 5)
 *  move(array, -1, 5)
 *  move([...array], 2, 5)
 */
export function move(array: any[], fromIndex: number, toIndex: number): any[] {
  if (array == null) return array
  fromIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex
  toIndex = toIndex < 0 ? array.length + toIndex : toIndex
  if ((fromIndex >= 0 && fromIndex < array.length) && (toIndex >= 0 && toIndex < array.length)) {
    const [item] = array.splice(fromIndex, 1)
    array.splice(toIndex, 0, item)
  }

  return array
}
